/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferedImage;

/**
 * Splash Screen
 * 根据不同的 mode 属性设置，也可以作为 About 窗体使用。
 * 
 * @author Chen Wei
 * @website www.chenwei.mobi
 * @email chenweionline@hotmail.com
 */
public class JSplashScreen extends javax.swing.JDialog {

    public static final int SPLASH_MODE = 0;
    public static final int ABOUT_MODE = 1;
    private Frame frame = null;
    private int mode = SPLASH_MODE;
    private BufferedImage image = null;

    public JSplashScreen(Frame frame, int mode, BufferedImage image) {

        if (frame == null) {
            throw new IllegalArgumentException("frame must be non null");
        }
        if (mode != SPLASH_MODE && mode != ABOUT_MODE) {
            throw new IllegalArgumentException("mode must be one of: SPLASH_MODE or ABOUT_MODE");
        }
        if (image == null) {
            throw new IllegalArgumentException("image must be non null");
        }

        this.frame = frame;
        this.mode = mode;
        this.image = image;

        if (mode == SPLASH_MODE) {
            frame.addComponentListener(new ComponentAdapter() {

                @Override
                public void componentShown(ComponentEvent e) {
                    JSplashScreen.this.dispose();
                }
            });
        } else {
            addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    JSplashScreen.this.dispose();
                }
            });
        }

        initComponents();

        setSize(new Dimension(image.getWidth(), image.getHeight()));
        setLocationRelativeTo(null);
    }

    /** */
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setUndecorated(true);
        getContentPane().setLayout(null);
    }// </editor-fold>

    @Override
    public void paint(Graphics g) {
        if (image != null) {
            g.drawImage(image, 0, 0, this);
        }
    }
}
